#include <windows.h>
#include "..\..\Resource\PipeClient\resource.h"

#include "../Pipe/stdclient.h"

BOOL	CALLBACK DialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);


static HWND g_HWND_DIALOG_PipeDemo_Client = NULL;

VOID	MyErrExit(LPSTR sz)
{
	MessageBoxA(g_HWND_DIALOG_PipeDemo_Client,sz,"ERROR",MB_ICONSTOP);
	ExitProcess(1);
	return;
}


static ST_PIPE_client	st_pipe_client = { 0 };

void Open(HWND hParDlg)
{
	
	DWORD	dwRetVal = 0 ;

	st_pipe_client.dwsize = sizeof(ST_PIPE_client);

	dwRetVal = msgclient_open(&st_pipe_client,sizeof(st_pipe_client));

	if (MSG_CLIENT_SUCCESS == dwRetVal )
	{
		EnableWindow(GetDlgItem(hParDlg,IDC_BUTTON_send),TRUE);
		MessageBox(hParDlg,TEXT("msgclient_open-> OK"),TEXT("demo for client pipe"),MB_OK);
	}
	else
	{
		MessageBox(hParDlg,TEXT("msgclient_open-> error "),TEXT("demo for client pipe"),MB_ICONSTOP);
	}


	return;
}

#ifdef UNICODE 
#define SNPRINTF _snwprintf
#else
#define SNPRINTF _snprintf
#endif /*UNICODE*/


void sendrequest(HWND hParDlg)
{
	TCHAR	szSendBuff[256] = { 0 };
	TCHAR	szGetBuff[256] = { 0 }; 
	TCHAR	szOther[512] = { 0 };
	DWORD	dwRetsize = 0 ;

	DWORD	dwRetval = 0 ;

	DWORD i =0;

	int nprintsize = 0; 
	
	GetDlgItemText(hParDlg,IDC_EDIT_INPUT,szSendBuff,sizeof(szSendBuff)/sizeof(TCHAR));
	
	for (i=0;i<10;++i)
	{
			
		nprintsize = SNPRINTF(szSendBuff,sizeof(szSendBuff),TEXT("client %u send byte"),i);
		
		dwRetval = msgclient_pipe(&st_pipe_client,szSendBuff,nprintsize,szGetBuff,sizeof(szGetBuff),&dwRetsize);

	}
	

	wsprintf(szOther,TEXT("retval=0x%08X retsize = %u"),dwRetval,dwRetsize);

	SetDlgItemText(hParDlg,IDC_EDIT_OTHERINFO,szOther );

	if ( MSG_CLIENT_SUCCESS == dwRetval )
	{

		SetDlgItemText(hParDlg,IDC_EDIT_RESPONSE,(char*)szGetBuff);

	}
	else
	{

		SetDlgItemText(hParDlg,IDC_EDIT_RESPONSE,TEXT("noresponse"));
	}


	return ;

}

void closemsg(HWND hParDlg)
{	
	DWORD	dwRetVal = 0 ;

	dwRetVal = msgclient_close(&st_pipe_client);

	EnableWindow(GetDlgItem(hParDlg,IDC_BUTTON_send),FALSE);

	if (MSG_CLIENT_SUCCESS == dwRetVal )
	{
		
		MessageBox(hParDlg,TEXT("msgclient_close-> OK"),TEXT("demo for client pipe"),MB_OK);
	}
	else
	{
		MessageBox(hParDlg,TEXT("msgclient_close-> error "),TEXT("demo for client pipe"),MB_ICONSTOP);
	}

	return;
}





int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	DialogBox (hInstance, MAKEINTRESOURCE(IDD_DIALOG_PipeDemo_Client), NULL, DialogProc);
	return 1 ;
}

BOOL CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_INITDIALOG:
			g_HWND_DIALOG_PipeDemo_Client = hDlg;
			return (TRUE);

		case WM_DESTROY:
		case WM_CLOSE:
			EndDialog(hDlg,0);
			return (TRUE);

		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDCANCEL:
					SendMessage(hDlg, WM_CLOSE, 0, 0);
					return (TRUE);

				case IDOK:
					//PipeClient();
					//SendMessage(hDlg, WM_CLOSE, 0, 0);
					return (TRUE);
				case IDC_BUTTON_send:
					sendrequest(hDlg);
					break;
				case IDC_BUTTON_connect:
					Open(hDlg);
					break;
				case IDC_BUTTON_close:
					closemsg(hDlg);

					break;
			}
		return (FALSE);
	}
	return (FALSE);
}





/*
WCHAR lpszPipename[] = {L"\\\\.\\pipe\\mynamedpipe"}; 

DWORD	PipeClient(VOID)
{ 
	HANDLE hPipe =NULL   ; 
	LPVOID lpvMessage = NULL; 
	CHAR chBuf[512]={0}; 
	BOOL fSuccess = 0; 
	DWORD cbRead=0, cbWritten=0, dwMode=0; 

 
	// Try to open a named pipe; wait for it, if necessary. 

	while (1) 
	{ 
		hPipe = CreateFileW( 
			lpszPipename,   // pipe name 
			GENERIC_READ |  // read and write access 
			GENERIC_WRITE, 
			0,              // no sharing 
			NULL,           // no security attributes
			OPEN_EXISTING,  // opens existing pipe 
			0,              // default attributes 
			NULL);          // no template file 

		// Break if the pipe handle is valid. 

		if (hPipe != INVALID_HANDLE_VALUE) 
		{
			break; 
		}

		// Exit if an error other than ERROR_PIPE_BUSY occurs. 

		if (GetLastError() != ERROR_PIPE_BUSY) 
		{
			MyErrExit("Could not open pipe"); 
		}

		// All pipe instances are busy, so wait for 20 seconds. 

		if (! WaitNamedPipeW(lpszPipename, 20000) ) 
		{
			MyErrExit("Could not open pipe"); 
		}
	} 

		// The pipe connected; change to message-read mode. 

		dwMode = PIPE_READMODE_MESSAGE; 
		fSuccess = SetNamedPipeHandleState( 
			hPipe,    // pipe handle 
			&dwMode,  // new pipe mode 
			NULL,     // don't set maximum bytes 
			NULL);    // don't set maximum time 

		if (!fSuccess) 
		{
			MyErrExit("SetNamedPipeHandleState"); 
		}

		// Send a message to the pipe server. 

		lpvMessage =  "default message"; 

		fSuccess = WriteFile( 
			hPipe,                  // pipe handle 
			lpvMessage,             // message 
			strlen(lpvMessage) + 1, // message length 
			&cbWritten,             // bytes written 
			NULL);                  // not overlapped 
		
		if (! fSuccess) 
		{
			MyErrExit("WriteFile"); 
		}

		do 
		{ 
			// Read from the pipe. 
				
			fSuccess = ReadFile( 
					hPipe,    // pipe handle 
					chBuf,    // buffer to receive reply 
					512,      // size of buffer 
					&cbRead,  // number of bytes read 
					NULL);    // not overlapped 

			if (! fSuccess && GetLastError() != ERROR_MORE_DATA) 
				break; 

			// Reply from the pipe is written to STDOUT. 

			if (! WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), 
					chBuf, cbRead, &cbWritten, NULL)) 
			{
				break; 
			}

		}
		while (! fSuccess);  // repeat loop if ERROR_MORE_DATA 

	CloseHandle(hPipe); 

	return 0; 
} 
*/